ProgressBar = {}

function ProgressBar.init (pbar, scr, x, y, long, haut, val, mode)
  local pb = tostring (pbar)
  
  _G[pb] = {}
  
  _G[pb].scr = scr
  
  _G[pb].x = x
  _G[pb].y = y
  
  _G[pb].long = long
  _G[pb].haut = haut
  
  _G[pb].valMax = 100
  _G[pb].val = val
  
  _G[pb].mode = mode
  
  _G[pb].x2 = _G[pb].x + _G[pb].long
  _G[pb].y2 = _G[pb].y + _G[pb].haut
  
  _G[pb].pr100 = _G[pb].long / 100
  _G[pb].pr200 = _G[pb].haut / 100
end

function ProgressBar.draw (pbar, skin)
  local pb = tostring (pbar)
  
  local skin = skin
  
  if _G[pb].mode == 1 then
    _G[pb].prval = _G[pb].val * _G[pb].pr100
    _G[pb].prX2 = _G[pb].x + _G[pb].prval
  elseif _G[pb].mode == 2 then
    _G[pb].prval = _G[pb].val * _G[pb].pr100
    _G[pb].prX2 = _G[pb].x2 - _G[pb].prval
  elseif _G[pb].mode == 3 then
    _G[pb].prval = _G[pb].val * _G[pb].pr200
    _G[pb].prY2 = _G[pb].y + _G[pb].prval
  elseif _G[pb].mode == 4 then
    _G[pb].prval = _G[pb].val * _G[pb].pr200
    _G[pb].prY2 = _G[pb].y2 - _G[pb].prval
  end
  
  screen.drawGradientRect (_G[pb].scr, _G[pb].x, _G[pb].y, _G[pb].x2, _G[pb].y2, c_grisclair, c_grisclair, c_blanc, c_blanc)
  
  if _G[pb].mode == 1 then
    if skin == nil then
      screen.drawGradientRect (_G[pb].scr, _G[pb].x, _G[pb].y, _G[pb].prX2, _G[pb].y2, c_blanc, c_blanc, c_vert, c_vert)
    else
      screen.drawGradientRect (_G[pb].scr, _G[pb].x, _G[pb].y, _G[pb].prX2, _G[pb].y2, c_blanc, c_blanc, skin, skin)
    end
  elseif _G[pb].mode == 2 then
    if skin == nil then
      screen.drawGradientRect (_G[pb].scr, _G[pb].x2, _G[pb].y, _G[pb].prX2, _G[pb].y2, c_blanc, c_blanc, c_vert, c_vert)
    else
      screen.drawGradientRect (_G[pb].scr, _G[pb].x2, _G[pb].y, _G[pb].prX2, _G[pb].y2, c_blanc, c_blanc, skin, skin)
    end
  elseif _G[pb].mode == 3 then
    if skin == nil then
      screen.drawGradientRect (_G[pb].scr, _G[pb].x, _G[pb].y, _G[pb].x2, _G[pb].prY2, c_blanc, c_blanc, c_vert, c_vert)
    else
      screen.drawGradientRect (_G[pb].scr, _G[pb].x, _G[pb].y, _G[pb].x2, _G[pb].prY2, c_blanc, c_blanc, skin, skin)
    end
  elseif _G[pb].mode == 4 then
    if skin == nil then
      screen.drawGradientRect (_G[pb].scr, _G[pb].x, _G[pb].y2, _G[pb].x2, _G[pb].prY2, c_blanc, c_blanc, c_vert, c_vert)
    else
      screen.drawGradientRect (_G[pb].scr, _G[pb].x, _G[pb].y2, _G[pb].x2, _G[pb].prY2, c_blanc, c_blanc, skin, skin)
    end
  end
  if _G[pb].val > _G[pb].valMax-1 then
    _G[pb].val = _G[pb].valMax
  end
  if _G[pb].val < 1 then
    _G[pb].val = 0
  end
end

function ProgressBar.addValue (pbar, val)
  local pb = tostring (pbar)
  
  _G[pb].val = _G[pb].val+val
end

function ProgressBar.subValue (pbar, val)
  local pb = tostring (pbar)
  
  _G[pb].val = _G[pb].val-val
end

function ProgressBar.value (pbar)
  local pb = tostring (pbar)
  
  return _G[pb].val
end

function ProgressBar.valueMax (pbar)
  local pb = tostring (pbar)
  
  return _G[pb].valMax
end

function ProgressBar.clear (pbar)
  local pb = tostring (pbar)
  
  _G[pbar] = nil
end

